<?php 

class UserModel extends BaseModel {

	function __construct() {
		parent::__construct();
	}

	function tableName() {
		return 'users';
	}

	function validateRule() {
		return $rule = array(
			'name' => array(
				'name' => '用户名',
				'rule' => '/[a-zA-Z]{6,16}/',
				'error' => '字母不区分大小写，6-16位',
			),
			'vcode' => array(
				'name' => '验证码',
				'rule' => '/[\da-zA-Z]{4}/',
				'error' => '非法的验证码',
			),
		);
	}

	function effectiveFieldsVaule($data) {
		$fields = ['name', 'password', 'status', 'salt', 'created_at', 'updated_at'];

		$insertData=array();
		foreach ($fields as $key => $value) {
			if(isset($data[$value]))
				$insertData[$value] = $data[$value];
		}

		return $insertData;
	}

	/*
	 * 插入一个新用户
	 */
	function insertNewMember($data) {
		$data['salt'] = $this->getSalt();
		$data['password'] = $this->createPassword($data['password'], $data['salt']);
		$data['updated_at'] = date('Y-m-d H:i:s');
		$data['created_at'] = date('Y-m-d H:i:s');

		$data = $this->effectiveFieldsVaule($data);

		if (empty($data))
			return false;

		return $this->insert($data);
	}

	function userLogin($data) {
		$userInfo = $this->getOneUserByEmail($data['name']);

		if (empty($userInfo)) {
			return '用户不存在';
		}

		return $this->checkUserStatus($data, $userInfo);
	}

	/*
	 * 通过邮箱获取用户
	 */
	function getOneUserByEmail($name) {
		return $this->where('name="'.$name.'"')->getOne();
	}

	/*
	 * 通过邮箱获取用户
	 */
	function getOneUserById($id) {
		return $this->where('id="'.$id.'"')->getOne();
	}

	/*
	 * 检查登录用户的状态
	 */
	function checkUserStatus($data, $userInfo) {

		if(!$this->comparePassword($this->createPassword($data['password'], $userInfo['salt']), $userInfo['password']))
			return '密码错误';

		switch ($userInfo['status']) {
			case 2:
				return '您是锁定用户，请联系管理员';
				break;
			
			case 3:
				return '您在该网站有劣迹，已被拉黑';
				break;
		}

		return $this->saveLoginInfo($userInfo);

	}

	/*
	 * 保存用户登录信息
	 */
	function saveLoginInfo($userInfo) {
		$_SESSION['user'] = $userInfo;
		unset($_SESSION['code']);
	}

	/*
	 * 清除用户登录信息
	 */
	function clearLoginInfo() {
		unset($_SESSION['user'], $_SESSION['userid']);
	}

	/*
	 * 检测数据的合法性
	 */
	function validate(&$data) {
		$error = true;
		foreach ($this->validateRule() as $key => $value) {
			if(!preg_match($value['rule'], $data[$key])){
				$error = $value['error'];
				break;
			}
		}

		if ($this->comparePassword()) 
			unset($data['repassword']);
		else
			$error = '密码不一致';
		

		return $error;
	}

	/*
	 * 检测数据在数据库内的合法性
	 */
	function checkDataInDatabase($data) {
		if($this->isExistsName($data['name']))
			return '姓名已存在';

		return true;
	}

	function comparePassword($oriPassword, $repeatPassword) {
		return $oriPassword==$repeatPassword;
	}

	/*
	 * 检测名称在数据库是否存在
	 */
	function isExistsName($username) {
		return $this->where('name="'.$username.'"')->count();
	}

	/*
	 * 检测邮箱在数据库是否存在
	 */
	function isExistsEmail($email) {
		return $this->where('email="'.$email.'"')->count();
	}

	/*
	 * 生成密码
	 */
	protected function createPassword($oriPassword, $salt) {
		return md5(md5($oriPassword).$salt);
	}

	/*
	 * 生成注册用户盐值
	 */
	private function getSalt() {
		$timeSalt = md5(time());
		return mb_substr($timeSalt, mt_rand(1, (strlen($timeSalt)-6)), 6);
	}

}